﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ActiveReportsDemo.Viewers
{
    public partial class ExportViewer : DemoBaseUserControl
    {
        public ExportViewer()
        {
            InitializeComponent();
            

            dlgSave = new SaveFileDialog();
            dlgSave.RestoreDirectory = false;
            dlgSave.AddExtension = true;
            
        }


        SaveFileDialog dlgSave = null;
        GrapeCity.ActiveReports.Document.PageDocument document = null;

        /// <summary>
        /// 显示报表
        /// </summary>
        /// <param name="name"></param>
        public override void ShowReport()
        {
            GrapeCity.ActiveReports.PageReport report1 = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(string.Format(@"Reports\{0}", CurrentReportName)));
            base.reportDesigner.Report = report1;
            document = new GrapeCity.ActiveReports.Document.PageDocument(report1);
            ReportViewer.LoadDocument(document);
            ReportWebBrowser.DocumentText = CommonFunction.getDescription(CurrentReportID);
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            try
            {
                dlgSave.Filter = "*.xlsx|.xlsx";
                dlgSave.FileName = "";
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    
                    GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();

                    XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
                    XlsExport1.Export(document, dlgSave.FileName);

                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);                 
                }
            }
            catch (Exception)
            {
                MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Application.DoEvents();            
            }
        }

        private void btnWord_Click(object sender, EventArgs e)
        {
            try
            {
                dlgSave.Filter = "*.doc|.doc";
                dlgSave.FileName = "";
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    GrapeCity.ActiveReports.Export.Word.Page.Settings settings = new GrapeCity.ActiveReports.Export.Word.Page.Settings();
                    

                    System.IO.FileInfo myFile = new System.IO.FileInfo(dlgSave.FileName);
                    
                    GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension renderingExtension  = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
                    GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(myFile.Directory,System.IO.Path.GetFileNameWithoutExtension(myFile.Name));
                    document.Render(renderingExtension ,_provider,settings);

                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Application.DoEvents();
            }
        }

        private void btnPdf_Click(object sender, EventArgs e)
        {
            try
            {
                dlgSave.Filter = "*.pdf|.pdf";
                dlgSave.FileName = "";
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    GrapeCity.ActiveReports.Export.Pdf.Page.Settings settings = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings();


                    System.IO.FileInfo myFile = new System.IO.FileInfo(dlgSave.FileName);
                    
                    GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
                    GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(myFile.Directory, System.IO.Path.GetFileNameWithoutExtension(myFile.Name));
                    document.Render(renderingExtension, _provider, settings);

                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Application.DoEvents();
            }
        }

        private void btnHtml_Click(object sender, EventArgs e)
        {
            try
            {
                dlgSave.Filter = "*.html|.html";
                dlgSave.FileName = "";
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    GrapeCity.ActiveReports.Export.Html.Page.Settings settings = new GrapeCity.ActiveReports.Export.Html.Page.Settings();


                    System.IO.FileInfo myFile = new System.IO.FileInfo(dlgSave.FileName);
                    
                    GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension renderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension();
                    GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(myFile.Directory, System.IO.Path.GetFileNameWithoutExtension(myFile.Name));
                    document.Render(renderingExtension, _provider, settings);

                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Application.DoEvents();
            }
        }

        private void btnText_Click(object sender, EventArgs e)
        {
            try
            {
                dlgSave.Filter = "*.txt|.txt";
                dlgSave.FileName = "";
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    GrapeCity.ActiveReports.Export.Xml.Section.TextExport txtExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
                    txtExport1.Encoding = Encoding.Unicode;

                    txtExport1.Export(document, dlgSave.FileName);
                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Application.DoEvents();
            }
        }

        private void btnCSV_Click(object sender, EventArgs e)
        {
            try
            {
                dlgSave.Filter = "*.csv|.csv";
                dlgSave.FileName = "";
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    GrapeCity.ActiveReports.Export.Xml.Section.TextExport csvExport1 = new GrapeCity.ActiveReports.Export.Xml.Section.TextExport();
                    csvExport1.Encoding = Encoding.Unicode;
                    csvExport1.TextDelimiter = "\t";
                    csvExport1.SuppressEmptyLines = true;
                    csvExport1.Export(document, dlgSave.FileName);
                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Application.DoEvents();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                dlgSave.Filter = "*.png|.png|*.bmp|.bmp|*.emf|.emf|*.gif|.gif|*.jpg|.jpg|*.tif|.tif";
                dlgSave.FileName = "";
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();

                    GrapeCity.ActiveReports.Export.Image.Page.Settings settings = new GrapeCity.ActiveReports.Export.Image.Page.Settings();
                    
                    switch (System.IO.Path.GetExtension(dlgSave.FileName).ToLower())
                    {
                        case ".png":
                            settings.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.PNG;
                            break;
                        case ".bmp":
                            settings.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.BMP;
                            break;
                        case ".emf":
                            settings.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.EMF;
                            break;
                        case ".gif":
                            settings.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.GIF;
                            break;
                        case ".jpg":
                            settings.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.JPEG;
                            break;
                        case ".tif":
                            settings.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.TIFF;
                            break;
                        default:
                            break;
                    }
                    settings.StartPage = 1;
                    settings.EndPage = 1;

                    System.IO.FileInfo myFile = new System.IO.FileInfo(dlgSave.FileName);
                    
                    GrapeCity.ActiveReports.Export.Image.Page.ImageRenderingExtension renderingExtension = new GrapeCity.ActiveReports.Export.Image.Page.ImageRenderingExtension();
                    GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(myFile.Directory, System.IO.Path.GetFileNameWithoutExtension(myFile.Name));
                    document.Render(renderingExtension, _provider, settings);

                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);                    
                }
            }
            catch (Exception)
            {
                MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Application.DoEvents();
            }
        }
    }
}
